<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Analysisresult extends Model{
	
	public $analysis_result = NULL;
		
	public function __construct($analysis_result_id=false){
		if ($analysis_result_id) $this->analysis_result = $this->get($analysis_result_id);
	}
	
	public function get($id=false){
		if ($id) return ORM::factory("dao_analysisresult")->find($id);
		return $this->analysis_result;
	}
		
	public function getDiastolicWaveAmp(){
		return $this->createParameter($this->analysis_result->aod_value,
									  $this->analysis_result->aod_x1,
									  $this->analysis_result->aod_y1,
									  $this->analysis_result->aod_x2,
									  $this->analysis_result->aod_y2);
	}
	
	public function getSystolicWaveLength(){
		return $this->createParameter($this->analysis_result->aos_value,
									  $this->analysis_result->aos_x1,
									  $this->analysis_result->aos_y1,
									  $this->analysis_result->aos_x2,
									  $this->analysis_result->aos_y2);
	}
	
	public function getRadialAugmentation(){
		return $this->createParameter($this->analysis_result->iar_value,
									  $this->analysis_result->iar_x1,
									  $this->analysis_result->iar_y1,
									  $this->analysis_result->iar_x2,
									  $this->analysis_result->iar_y2); 
	}
	
	public function getSystolicReflexOffset(){
		return $this->createParameter($this->analysis_result->t_value,
									  $this->analysis_result->t_x1,
									  $this->analysis_result->t_y1,
									  $this->analysis_result->t_x2,
									  $this->analysis_result->t_y2);
	}
	
	public function getHeartRate(){
		//return $this->analysis_result->heart_rate;
		return 75.60;
	}
	
	private function createParameter($value,$x1,$y1,$x2,$y2){
		$param = Model::factory("segmentparameter");
		try{
			$param->setValue($value);
			$param->setStartPoint($x1,$y1);
			$param->setEndPoint($x2,$y2);
		}catch(Exception $e){
			Fire::log("Valor nulo : ".$e->getMessage());
		}
		return $param;
	}
	
	public function exportResults($filenamepath){
		
		$contenido = "IAR[%] = ".$this->analysis_result->iar_value."\r\n";
		$contenido.= "AOD[%] = ".$this->analysis_result->aod_value."\r\n";
		$contenido.= "AOS[ms] = ".$this->analysis_result->aos_value."\r\n";
		$contenido.= "Retardo[ms] = ".$this->analysis_result->t_value."\r\n";
		$contenido.= "Frecuencia Cardiaca[ppm] = ".$this->analysis_result->heart_rate;
		//implode("\r\n",$this->getSamples());
		$f=fopen($filenamepath,"w");
		fwrite($f,$contenido);
		fclose($f);
	}
	
	
	
	public function save($id, array $data){
		
		$this->analysis_result = ($id) ? ORM::factory("dao_analysisresult",$id) : ORM::factory("dao_analysisresult");
		
		$this->analysis_result->aod_x1 = floatval($data["aod_x1"]);
		$this->analysis_result->aod_y1 = floatval($data["aod_y1"]);
		$this->analysis_result->aod_x2 = floatval($data["aod_x2"]);
		$this->analysis_result->aod_y2 = floatval($data["aod_y2"]);
		$this->analysis_result->aod_value = floatval($data["aod_y2"]) - floatval($data["aod_y1"]);
		
		$this->analysis_result->aos_x1 = floatval($data["aos_x1"]);
		$this->analysis_result->aos_y1 = floatval($data["aos_y1"]);
		$this->analysis_result->aos_x2 = floatval($data["aos_x2"]);
		$this->analysis_result->aos_y2 = floatval($data["aos_y2"]);
		$this->analysis_result->aos_value = floatval($data["aos_x2"]) - floatval($data["aos_x1"]);
		
		$this->analysis_result->iar_x1 = floatval($data["iar_x1"]);
		$this->analysis_result->iar_y1 = floatval($data["iar_y1"]);
		$this->analysis_result->iar_x2 = floatval($data["iar_x2"]);
		$this->analysis_result->iar_y2 = floatval($data["iar_y2"]);
		$this->analysis_result->iar_value = floatval($data["iar_y2"]) - floatval($data["iar_y1"]);
		
		$this->analysis_result->t_x1 = floatval($data["t_x1"]);
		$this->analysis_result->t_y1 = floatval($data["t_y1"]);
		$this->analysis_result->t_x2 = floatval($data["t_x2"]);
		$this->analysis_result->t_y2 = floatval($data["t_y2"]);
		$this->analysis_result->t_value = floatval($data["t_x2"]) - floatval($data["t_x1"]);
		
		$this->analysis_result->heart_rate = floatval($data["heart_rate"]);
		
		$this->analysis_result->save();
		
		return $this->analysis_result->id;
	}
	
	public function delete($id){
		/*$this->rawsignal = ORM::factory("dao_rawsignal",$id);
		$this->rawsignal->delete();
		return "Registro: ".$this->rawsignal->id." eliminado con exito";*/
	}
	
	
}
?>